<?php
if ($uid == 180995)
{
	file_put_contents(S_ROOT."cache/qhb.txt",$_SERVER["REQUEST_URI"]."\r\n",FILE_APPEND);
}
$memcache_obj = get_memcache();
$uid = $_COOKIE['uid'];
$uid = $memcache_obj->get("user_".$uid);
if(!$uid){
    header("Location: /wx.php?tp=login");
}
$tpl->assign('uid',$uid);

$op = $_GET['op'];
$appid = 98;
if ($op == 'chai')
{
	require_once 'source/data/commend.php';
	$tpl->assign("commendgoods",$commendgoods);
	if ($uid == '139776')
	{
		file_put_contents(S_ROOT."cache/qhb.txt",$_SERVER["REQUEST_URI"]."\r\n",FILE_APPEND);
	}	
	
	$timestr = (int)$_GET['timestr'];
	if (empty($timestr))
	{
		echo echojson('操作异常',0,'');exit;
	}
	$total_num = 1000;
	$currentstr = date('Ymd');
	$opentime = $_GET['opentime'];
	$id = (int)$_GET['id'];
	$sql = "select id,left_num from tttj.qhb_list where datestr=$timestr and opentime='$opentime'";
	$qinfo = getonerecord($sql);
	if ($id==0 || $id!=$qinfo['id'])
	{
		echo echojson('操作异常',0,'');exit;
	}
	$yiqiang = $total_num - $qinfo['left_num'];
	if ($timestr == $currentstr)
	{
		$sql = "select * from tttj.userinfo where id=$uid";
		$uidexists = getonerecord($sql);
		if (!$uidexists)
		{
			echo echojson('用户不存在',0,'');exit;
		}
		$nowtime = date('Hi');
		if ($nowtime>='1830')
		{
			$curropentime = '1830';
		}elseif ($nowtime>='1330'){
			$curropentime = '1330';
		}elseif ($nowtime>='1030'){
			$curropentime = '1030';
		}else {
			$curropentime = '1830';
		}
		if ($opentime == $curropentime)
		{
			$sql = "select id from tttj.qhb_log where uid=$uid and qhb_id=$id";
			$qiangguo = getonerecord($sql);
			if (!$qiangguo && $qinfo['left_num']>0)
			{
				$sql = "select total_num-use_num as left_num from tttj.qhb_chance where uid=$uid limit 1";
				$chanceinfo = getonerecord($sql);
				if ((!$chanceinfo) || $chanceinfo['left_num']<=0)
				{
					$dstatus = 3;
				}else {
					$sql = "select id,cash from tttj.qhb_pool where daystr='$currentstr' and dstatus=1 order by rand() limit 1";
					$hbinfo = getonerecord($sql);
					$cash = $hbinfo['cash'];
					$hbid = $hbinfo['id'];
					addfinance($uid, 3, $cash, 1, '抢红包');
					$time = time();
					$sql = "update tttj.qhb_list set left_num=left_num-1 where id=$id";
					$issuccess = exec_db($sql);
					if (!$issuccess)
					{
						echo echojson('网络异常',0,'');exit;
					}
					$sql = "update tttj.qhb_pool set dstatus=2 where id=$id";
					exec_db($sql);
					$sql = "update tttj.qhb_chance set use_num=use_num+1 where uid=$uid";
					exec_db($sql);
					$sql = "insert into tttj.qhb_log (qhb_id,hb_id,hb_cash,uid,ontime,appid) values ($id,$hbid,$cash,$uid,$time,$appid)";
					exec_db($sql);
					// 获取抢红包纪录
					$sql = "select uid,ontime,hb_cash from tttj.qhb_log where qhb_id=$id order by id desc limit 50";
					$res = exec_db($sql);
					while ($row = mysql_fetch_assoc($res))
					{
						$row['ontime'] = date('Y-m-d H:i:s',$row['ontime']);
						$list[] = $row;
					}
					$tpl->assign('yiqiang',$yiqiang+1);
					$tpl->assign('mycash',$cash);
					$tpl->assign('records',$list);
					$tpl->display("wx/templates/{$tp}_detail_new.html");exit;
					//$tpl->display("{$tp}_detail.html");exit;
				}
			}else {
				$dstatus = 1;
			}
		}
	}
	$tpl->assign('yiqiang',$yiqiang);
	$sql = "select hb_cash from tttj.qhb_log where uid=$uid and qhb_id=$id";
	$myinfo = getonerecord($sql);
	if (!$myinfo && $dstatus==0)
	{
		$dstatus = 2;
	}
	$tpl->assign('dstatus',$dstatus);
	$mycash = $myinfo['hb_cash'];
	$tpl->assign('mycash',$mycash);
	$sql = "select uid,ontime,hb_cash from tttj.qhb_log where qhb_id=$id order by hb_cash desc limit 1";
	$frist = getonerecord($sql);
	$sql = "select uid,ontime,hb_cash from tttj.qhb_log where qhb_id=$id order by id desc limit 50";
	$res = exec_db($sql);
	while ($row = mysql_fetch_assoc($res))
	{
		$record['uid'] = $row['uid'];
		$record['hb_cash'] = $row['hb_cash'];
		$record['ontime'] = date('Y-m-d H:i:s',$row['ontime']);
		$list[] = $record;
	}
	foreach($list as $k=>$v){
		if($v['uid'] == $frist['uid']){
			$list[$k]['class'] = '1';
		}
	}
    
	$tpl->assign('records',$list);
	$tpl->display("wx/templates/{$tp}_detail_new.html");exit;
	//$tpl->display("{$tp}_detail.html");exit;
}	

$sql = "select total_num-use_num as left_num from tttj.qhb_chance where uid=$uid limit 1";
$chanceinfo = getonerecord($sql);
if ($chanceinfo && $chanceinfo['left_num']>0)
{
	$leftnum = $chanceinfo['left_num'];
}else {
	$leftnum = 0;
}
$tpl->assign('leftnum',$leftnum);
$appname = getappname($appid);
$tpl->assign("appname",$appname);
$tpl->assign('appid',$appid);
$tpl->display("wx/templates/{$tp}.html");
?>